$(function() {

	var articleId = common.getParam('article_id');

	/**
	 * 显示博文详情
	 */
	
	var $artileDetail = $('#artileDetail');
	common.ajax($artileDetail, 'getArticleInfo?article_id=' + articleId, function(data) {
		common.render($artileDetail, $('#artileDetailTpl'), {
			article: data.result
		});
	});

	/**
	 * 显示博文的评论信息
	 */
	
	var $disscuss = $('#disscuss');
	
	function listDiscuss(){
		common.ajax($disscuss, 'listDiscuss?article_id=' + articleId, function(data) {
			common.render($disscuss, $('#discussTpl'), {
				disscusses: data.result
			});
			
			// 等待评论信息显示完成才能获取评论信息
			
		});
	}
	listDiscuss();
	
	/**
	 * 对指定的评论点赞或否定（点赞或者否定，type为true表示点赞，type为false表示否定）
	 */
	
	var $agreeShow;
	var preAgreeNumber = 0;
	$(document).one('click', '.fa-thumbs-o-up', agree);
	
	function agree(){
		var token = common.checkToken();
		if(!token){
			return;
		}
		var $that = $(this);
		$agreeShow = $that.children('span');
		var param = {
			type:true;
			discuss_id:$(this).date('discuss'),
			token = token;
			time: new Date().getTime()
		};
		$.get(common.path + 'agreeOrArgue',param,function(date){
			if(date.code == 401){
				layer.msg('请先登录！');
			}else{
				if(date.result){
					preAgreeNumber = Number($agreeShow.html()) + 1;
					$agreeShow.html('已评');
					$that.css('cursor','default');
					$(document).off('click', '.fa-thumbs-o-up');
				}
			}
		})
	}
	$(document).on('mouseenter', '.fa-thumbs-o-up', function(){
		var $that = $(this);
		$agreeShow = $that.children('span');
		preAgreeNumber = Number($agreeShow.html());
		
		var param = {
			discuss_id:$(this).date('discussid'),
			token = sessionStorage.getItem('token');
			time: new Date().getTime()
		};
		$.get(common.path + 'isOpinion',param, function(date){
			if(date.reason == '已评'){
				$agreeShow.html('已评');
				$that.css('cursor','default');
				$(document).off('click', '.fa-thumbs-o-up');
			}
		})
	});
	$(document).on('mouseleave', '.fa-thumbs-o-up', function(){
		$agreeShow.html(preAgreeNumber);
		$(document).one('click', '.fa-thumbs-o-up', agree);
	});
	
	var $argueShow;
	var preAgreeNumber = 0;
	$(document).on('click', '.fa-thumbs-o-down', argue);
	
	function argue(){
		var token = common.checktoken();
		if(!token){
			return;
		}
		
		var $that = $(this);
		$argueShow = $that.children('span');
		var param = {
			type:false;
			discuss_id:$(this).date('discussid'),
			token = token;
			time: new Date().getTime()
		};
		$.get(common.path + 'agreeOrArgue',param,function(date){
			if(date.code == 401){
				layer.msg('请先登录！');
			}else{
				if(date.result){
					preAgreeNumber = Number($agreeShow.html()) + 1;
					$agreeShow.html('已评');
					$that.css('cursor','default');
					$(document).off('click', '.fa-thumbs-o-down');
				}
			}
		})
	};
	$(document).on('mouseenter', '.fa-thumbs-o-down	', function(){
		var $that = $(this);
		$argueShow = $that.children('span');
		preAgreeNumber = Number($agreeShow.html());
		
		var param = {
			discuss_id:$(this).date('discussid'),
			token = sessionStorage.getItem('token');
			time: new Date().getTime()
		};
		$.get(common.path + 'isOpinion',param, function(date){
			if(date.reason == '已评'){
				$agreeShow.html('已评');
				$that.css('cursor','default');
				$(document).off('click', '.fa-thumbs-o-down');
			}
		})
	});
	$(document).on('mouseleave', '.fa-thumbs-o-down', function(){
		$agreeShow.html(preAgreeNumber);
		$(document).one('click', '.fa-thumbs-o-down', argue);
	});
	
	/**
	 *发表评论
	 */
	
	var $discussContent = $('#discussContent');
	$('#discussContent').click(function(){
		var $that = $(this);
		var token = common.checkToken();
		if(!token){
			return;
		}
		
		//防止重复提交
		$that.prop('disabled',true);
		$that.html('正在发表评论。。。');
		
		var	param = {
			'token':token;
			'discuss.article_id':articleId,
			'discuss.content':$discussContent.val().trim()
		};
		$.post(common.path + 'addDiscuss',param,function(date){
			if(date.code == 401){
				layer.msg('请先登录！');
			}else if (date.code == 412){
				layer.msg(date.reason);
			}else{
				$discuss.children('li:gt(0)').remove();
				listDiscuss();
				$disucssContent.val('');
				layer.msg('评论成功！');
			}
			$that.prop('disabled',false);
			$that.html('发表');
		});
	});
});	